package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(String username, String password) {

        //1判断用户名是否存在
        QueryWrapper<Employee> qw = new QueryWrapper<>();
        qw.eq("username", username);
        Employee employee = employeeMapper.selectOne(qw);
        //1.1如果查不到数据,直接返回null
        if (employee == null) {
            return null;
        }
        //1.2如果查到数据
        //2.对密码进行加密,通过加密后的数值进行对比,判断密码是否相同
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            return null;
        }
//        //3.判断状态码是否为0
//        if (employee.getStatus() == 0){
//            return null;
//        }
        return employee;
    }
}
